<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="策略名称" prop="policyName">
        <el-input
          v-model="queryParams.policyName"
          placeholder="请输入策略名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:policy:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:policy:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:policy:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:policy:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="policyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="策略ID" align="center" prop="policyId"/>
      <el-table-column label="策略名称" align="center" prop="policyName"/>
      <el-table-column label="执行方式" align="center" prop="exeMethon">
        <template slot-scope="scope">
          <span v-if="scope.row.exeMethon == 1">手动</span>
          <span v-else>自动</span>
        </template>
      </el-table-column>
      <el-table-column label="开启复杂密码" align="center" prop="openComplex">
        <template slot-scope="scope">
          <span v-if="scope.row.openComplex == 'true'" style="color: lightgreen">开启</span>
          <span v-else style="color: red">未开启</span>
        </template>
      </el-table-column>
      <el-table-column label="密码最小长度" align="center" prop="passMin"/>
      <el-table-column label="必密规则" align="center" prop="passRule">
        <template slot-scope="scope">
          <span v-if="scope.row.passRule == 1">导入</span>
          <span v-else-if="scope.row.passRule == 2">数字密码</span>
          <span v-else-if="scope.row.passRule == 3">数字+字母</span>
          <span v-else>数字+字母+符号</span>
        </template>
      </el-table-column>
      <el-table-column label="特权改密" align="center" prop="usePrivilege">
        <template slot-scope="scope">
          <span v-if="scope.row.usePrivilege == 'true'" style="color: lightgreen">开启</span>
          <span v-else style="color: red">未开启</span>
        </template>

      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="0"
            inactive-value="1"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>

      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:policy:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:policy:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改策略管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="策略名称" prop="policyName">
          <el-input v-model="form.policyName" placeholder="请输入策略名称"/>
        </el-form-item>
        <el-form-item label="执行方式" prop="exeMethon">
          <el-select v-model="form.exeMethon" placeholder="请选择执行方式" value-key="index" clearable
                     :style="{width: '100%'}">
            <el-option v-for="(item, index) in field101Options" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生成规则" prop="passRule">
          <el-select v-model="form.passRule" placeholder="请选择生成规则" clearable :style="{width: '100%'}"
                     @change="changeOnDutyHeaderBB">
            <el-option v-for="(item, index) in field102Options" :key="index" :label="item.label"
                       :value="item.value" :disabled="item.disabled"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item v-show="see" label="复杂密码" prop="openComplex" required>
          <el-switch v-model="form.openComplex"></el-switch>
        </el-form-item>
        <el-form-item v-show="see" label="密码长度" prop="passMin">
          <el-input v-model="form.passMin" placeholder="请输入密码最小长度"/>
        </el-form-item>
        <el-form-item v-show="see" label="特权改密" prop="usePrivilege" required>
          <el-switch v-model="form.usePrivilege"></el-switch>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {addPolicy, delPolicy, getPolicy, listPolicy, updatePolicy} from "@/api/system/policy";
  import {changePolicyStatus} from "../../../api/system/policy";

  export default {
    name: "Policy",
    data() {
      return {
        // 遮罩层
        loading: true,

        // 是否显示
        see: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 策略管理表格数据
        policyList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          policyName: null,
          exeMethon: null,
          openComplex: null,
          passMin: null,
          passRule: null,
          usePrivilege: null,
          status: null,
        },
        // 表单参数
        form: {
          policyName: null,
          exeMethon: 1,
          openComplex: null,
          passMin: null,
          passRule: 1,
          usePrivilege: null,
          status: null,
        },
        field101Options: [{
          "label": "手动执行",
          "value": 1
        }, {
          "label": "自动执行",
          "value": 2
        }],
        field102Options: [
          {
            "label": "用户导入",
            "value": 1
          }, {
            "label": "数字密码",
            "value": 2
          }, {
            "label": "数字+字母",
            "value": 3
          }, {
            "label": "数字+字母+符号",
            "value": 4
          }],
        // 表单校验
        rules: {
          policyName: [
            {required: true, message: "策略名称不能为空", trigger: "blur"}
          ],
          exeMethon: [
            {required: true, message: "请选择下拉选择", trigger: "change"}
          ],
          passRule: [
            {required: true, message: "请选择下拉选择", trigger: "change"}
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    beforeMount: function () {
      //this.projectValue = Number("1");
    },
    methods: {
      /** 查询策略管理列表 */
      getList() {
        this.loading = true;
        listPolicy(this.queryParams).then(response => {
          this.policyList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      changeOnDutyHeaderBB(data) {

        if(1==data){
          this.see = false;
        }else{
          this.see = true;
        }
        //console.log(data);
      },
      // 用户状态修改
      handleStatusChange(row) {
        let text = row.status === "0" ? "启用" : "停用";
        this.$modal.confirm('确认要"' + text + '""' + row.policyName + '"用户吗？').then(function () {
          return changePolicyStatus(row.policyId, row.status);
        }).then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          policyId: null,
          policyName: null,
          exeMethon: null,
          openComplex: null,
          passMin: null,
          passRule: null,
          usePrivilege: null,
          status: "0",
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.policyId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加策略管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const policyId = row.policyId || this.ids
        getPolicy(policyId).then(response => {
          console.log(JSON.stringify(response.data));

          this.form = {
            policyName: null,
            exeMethon: null,
            openComplex: null,
            passMin: null,
            passRule: null,
            usePrivilege: null,
            status: null,
          };

          this.form = response.data;
          //*绑定值*//
          this.form.exeMethon = Number(response.data.exeMethon);
          this.form.passRule = Number(response.data.passRule);


          console.log(">>>>" + JSON.stringify(response.data.openComplex));
          console.log(">>>>" + JSON.stringify(response.data.usePrivilege));
          if (response.data.openComplex == "true") {
            this.form.openComplex = true;
          } else {
            this.form.openComplex = false;
          }

          if (response.data.usePrivilege == "true") {
            this.form.usePrivilege = true;
          } else {
            this.form.usePrivilege = false;
          }

          //*绑定值*//
          this.open = true;
          this.title = "修改策略管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.policyId != null) {
              updatePolicy(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPolicy(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const policyIds = row.policyId || this.ids;
        this.$modal.confirm('是否确认删除策略管理编号为"' + policyIds + '"的数据项？').then(function () {
          return delPolicy(policyIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/policy/export', {
          ...this.queryParams
        }, `policy_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
